package com.welike.shibo.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.gson.JsonObject;
import com.welike.shibo.eneity.Msg;
import com.welike.shibo.eneity.PushBean;
import com.welike.shibo.eneity.Zan;
import com.welike.shibo.mapper.MsgMapper;
import com.welike.shibo.service.*;
import com.welike.shibo.vo.page.ZanMsgPageVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 消息中心
 *
 * @author fangyu
 * @version v1.0.0
 * @since 2019-08-20 00:40
 */
@Slf4j
@Service
public class MsgServiceImpl extends ServiceImpl<MsgMapper, Msg> implements MsgService {

    @Resource
    private MsgMapper msgMapper;

    @Resource
    private VideoService videoService;

    @Resource
    private ZanService zanService;

    @Resource
    private UserService userService;

    @Resource
    private JPushService jPushService;

    @Resource
    private PushService pushService;


    /**
     * 查询用户作品被点赞的消息
     *
     * @param uid 用户uid
     * @return List
     */
    @Override
    public List<ZanMsgPageVo> queryZanMsg(int page, int pageSize, String uid) {
        Page<Zan> p = new Page<>();
        List<ZanMsgPageVo> zanMsgPageVoList = new ArrayList<>();

        // 一、先查询自己发布的作品
        List<String> result = videoService.findVideoIdByUid(uid);
        // 如果发布作品为空则直接返回
        if (result.size() == 0) {
            return zanMsgPageVoList;
        }
        log.info("result ==> {}", result);
        // 二、查询所有作品被点赞的信息 分页（最新查询10条）
        IPage<Zan> zanIPage = zanService.queryZanInVidList(p, result);

        for (Zan zan : zanIPage.getRecords()) {
            ZanMsgPageVo zanMsgPageVo = new ZanMsgPageVo();
            zanMsgPageVo.setUid(zan.getUserId());
            zanMsgPageVo.setNickName(userService.queryNickNameByUid(zan.getUserId()));
            zanMsgPageVo.setAvatar(userService.queryAvatarByUid(zan.getUserId()));
            zanMsgPageVo.setVid(zan.getZanObjId());
            zanMsgPageVo.setVideoUrl(videoService.queryVideoUrlByVid(zan.getZanObjId()));
            zanMsgPageVo.setCover(videoService.queryCoverByVid(zan.getZanObjId()));
            zanMsgPageVo.setCreateTime(zan.getCreateTime());
            zanMsgPageVoList.add(zanMsgPageVo);
        }
        return zanMsgPageVoList;
    }

    @Override
    public IPage<Msg> queryAllPushMsg(int page, int pageSize, String uid) {
        Page<Msg> p = new Page<>(page, pageSize);
        return msgMapper.queryAllByUserIdOrderByCreateTimeDesc(p, uid);
    }

    /**
     * @param push_type 推送类型
     * @param push_uid  被推送用户id
     * @param title     推送标题
     * @param content   推送内容
     */
    @Override
    public boolean pushMsg(int push_type, String push_uid, String title, String content, JsonObject jsonObject) {
        boolean push_switch = pushService.querySwitch(push_type, push_uid);
        if (!push_switch) {
            log.info("推送开关被用户关闭，推送失败");
            return false;
        }
        log.info("push_type==>{},push_uid==>{},nickname==>{}", push_type, push_uid, title);
        String register = userService.queryDeviceIdByUid(push_uid);
        log.info("push_id =>{}", register);
        String platform = userService.queryDeviceTypeByUid(push_uid);
        log.info("platform =>{}", platform);
        boolean flag = false;
        if (register == null) {
            log.info("推送失败, register为空");
            return false;
        }
        PushBean pushBean = new PushBean();
        pushBean.setTitle(title);
        pushBean.setAlert(content);
        try {
            if ("ios".equals(platform)) {
                flag = jPushService.pushIOS(pushBean, push_type, jsonObject, register);
            } else if ("android".equals(platform)) {
                if (content == null){
                    pushBean.setTitle("亲您好，您收到了一条新的消息");
                    pushBean.setAlert(title);
                }
                Map<String, String> extras = new HashMap<>();
                extras.put("push_type", String.valueOf(push_type));
                pushBean.setExtras(extras);
                flag = jPushService.pushAndroid(pushBean, register);
            }
        } catch (Exception e){
            flag = false;
            log.error("推送异常==>{}", e.getMessage());
        }

        if (flag) {
            log.info("推送成功");
            return true;
        } else {
            log.info("推送失败");
            return false;
        }
    }
}
